﻿(function ($) {

    var options = {
        id: '',
        enableSanitizing: 'false',
        previewMode: 'toolbar',
        helpUrl: 'http://daringfireball.net/projects/markdown/syntax'
    };

    var converter, editor, obj;

    var methods = {
        init: function (settings) {
            $.extend(options, {
                id: obj.attr("id").substring(10)
            });
            
            // Only create editor if one is not already registered
            if (obj.siblings(".wmd-button-bar").find(".wmd-button").length == 0) {
                $.extend(options, settings);
            
                converter = options.enableSanitizing
                            ? Markdown.getSanitizingConverter()
                            : new Markdown.Converter();
                editor = new Markdown.Editor(converter, options.id);

                if (options.previewMode == "toolbar") {
                    obj.siblings(".wmd-preview").hide();
                }
                else if (options.previewMode == "above") {
                    obj.siblings(".wmd-preview").insertBefore(obj.siblings('.wmd-button-bar'));
                }
                else if (options.previewMode == "") {
                    obj.siblings(".wmd-preview").hide();
                }

                if (options.helpUrl) {
                    var help = function () {
                        window.open(options.helpUrl, 'MarkDown Help');
                    };

                    editor = new Markdown.Editor(converter, "-" + options.id, { handler: help });
                }

                // Create editor
                editor.run();
            
                // Insert menu item for preview switching
                if (options.previewMode == "toolbar") {
                    obj.siblings('.wmd-button-bar').find(".wmd-button").last().after("<li class='wmd-button wmd-preview-button' title='Preview'><span>&nbsp;</span></li>");
                    $(".wmd-preview-button", obj.siblings('.wmd-button-bar')).toggle(function () {
                        obj.pagedown('preview');
                        $(this).addClass("edit");
                    
                        return false;
                    }, function () {
                        obj.pagedown('edit');
                        $(this).removeClass("edit");
                    
                        return false;
                    });
                }
            }
        },
        preview: function () {
            // Switch to preview mode
            obj.hide();
            obj.siblings(".wmd-preview").show();
        },
        edit: function () {
            // Switch to edit mode
            obj.show();
            obj.siblings(".wmd-preview").hide();
        }
    };

    $.fn.pagedown = function (method) {

        obj = this;

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        }
        else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };

})(jQuery);